package Tools;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import classes.AdminDB;
import classes.user;
import Admin.MailSender;
import classes.BaseServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

/**
 *
 * @author Alvaro
 */
@WebServlet(name="signup_process1", urlPatterns={"/signup_process1"})
public class signup_process1 extends BaseServlet {
   
    
     RequestDispatcher rd;

    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException{

        //Email del usuario que se quiere registrar
        String emailSMS = request.getParameter("email");
        
        
        HttpSession s = request.getSession(true);
        AdminDB adb = new AdminDB();
        user c = new user();
        boolean valid=false;
        
        //Parametros enviados en el formulario
        c.setName(request.getParameter("name") != null ? request.getParameter("name") : "");
        c.setSecondname(request.getParameter("secondname") != null ? request.getParameter("secondname") : "");
        c.setEmail(request.getParameter("email") != null ? request.getParameter("email") : "");
        c.setMoney(10000);
        c.setCountry(request.getParameter("country") != null ? request.getParameter("country") : "");
        c.setCity(request.getParameter("city") != null ? request.getParameter("city") : "");
        c.setZip(request.getParameter("zip") != null ? request.getParameter("zip") : "");
        c.setStreet(request.getParameter("street") != null ? request.getParameter("street") : "");
        c.setNumber(request.getParameter("number") != null ? request.getParameter("number") : "");
        c.setFloor(request.getParameter("floor") != null ? request.getParameter("floor") : "");
        c.setLetter(request.getParameter("letter") != null ? request.getParameter("letter") : "");
        c.setPass(request.getParameter("pass") != null ? request.getParameter("pass") : "");
        c.setDir("images/userphotos/nophoto.jpg");
        c.setPurchases(0);

        AdminDB conn = new AdminDB();
        
             
        //Se comprueba si el email con el que el usuario se quiere registrar ya tiene cuenta en nuestra pagina
        if(adb.existeUsuario(conn.getConnection(),emailSMS)==false){
            //Si el email aun no existe, se registra
            c.guardar();
            System.out.println("Registrando email: "+emailSMS);
            
            //Enviamos el email al usuario
            MailSender ms = new MailSender();
            ms.send("alvarof1store@gmail.com", emailSMS, "Gracias por su confianza! Esta es su informacion de registro\n"
                    + "Nombre: "+request.getParameter("name") +" \nApellido: "+request.getParameter("secondname") +" \n"
                    + "Country: "+request.getParameter("country") +" \nCity: "+request.getParameter("city") +" \n"
                    + "Street: "+request.getParameter("street") +" \nNumber: "+request.getParameter("number") +" \n"
                    + "Floor and letter: "+request.getParameter("floor")+" "+request.getParameter("letter") +" \n"
                    + "Password: "+request.getParameter("pass"), "Sign up info");
            
            //Redireccion
            dispatch("/WEB-INF/private/signupok.jsp", request, response);
           
           
        } 
        //Si el usuario ya esta registrado se informa
        else {
            dispatch("/WEB-INF/private/errorSignup.jsp", request, response);

        }
      
        
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(signup_process1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
   @Override
   protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(signup_process1.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
